package com.wish.g04.financing.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wish.g04.base.entity.CwClearing;
import com.wish.g04.base.service.ICwClearingService;
import com.wish.g04.common.core.utils.StringUtils;
import com.wish.g04.financing.service.ClearingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 结算表 服务实现类
 * @author tyd
 * @since 2022-05-29
 */
@Service
public class ClearingServiceImpl implements ClearingService {

    @Autowired
    private ICwClearingService iCwClearingService;

    /**
     * 查询所有结算信息
     * @param cwClearing
     * @return
     */
    @Override
    public List<CwClearing> getClearingList(CwClearing cwClearing) {
        QueryWrapper<CwClearing> wrapper = new QueryWrapper<>();
        //只显示未结算的信息
        wrapper.eq("clearing_confirm_state",1);
        if (StringUtils.isNotEmpty(cwClearing.getCheckBatchNo())) {
            wrapper.eq("check_batch_no",cwClearing.getCheckBatchNo());
        }
        return iCwClearingService.list(wrapper);
    }

    /**
     * 确认结算
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean notarizeClearing(List<Integer> ids) {
        //处理接收的数据为空的情况
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        ids.forEach(id->{
            UpdateWrapper<CwClearing> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id",id);
            updateWrapper.set("clearing_confirm_state",2);
            boolean update = iCwClearingService.update(updateWrapper);
        });
        return true;
    }
}
